{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "2b89e524-13e4-408e-b402-abe57f148d7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ddd [ True False False False False]\n",
      "[0 2]\n",
      "[3 2]\n",
      "[3 2]\n",
      "[2 3]\n",
      "[2 3]\n",
      "[0 1 1 0 0] [ True False False  True  True] [False  True  True False False]\n",
      "[['A' 'B' 'C']\n",
      " ['A' 'C' 'B']\n",
      " ['A' 'A' 'B']]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy import stats\n",
    "\n",
    "data = np.array([['A', 'B', 'C'], \n",
    "                    ['B', 'C', 'A'], \n",
    "                    ['C', 'A', 'B'], \n",
    "                    ['A', 'C', 'B'], \n",
    "                    ['A', 'A', 'B']]) \n",
    "modes = [['A', 'B', 'C'], \n",
    "            ['C', 'B', 'A']] \n",
    "\n",
    "clusters = np.zeros(data.shape[0], dtype=int) \n",
    "print('ddd', clusters ==  [0,1,2,3,4])\n",
    "for i, obj in enumerate(data):\n",
    "    # consider True as 1, False as 0\n",
    "    dis = np.array([sum(obj != mode) for mode in modes])\n",
    "    print(dis)\n",
    "    clusters[i] = np.argmin(dis)\n",
    "    \n",
    "print(clusters, clusters == 0, clusters == 1)\n",
    "a = data[clusters == 0]\n",
    "print(a)\n",
    "#stats.mode(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "b2fedc7b-117d-453e-870e-90d8fd8cb584",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 3 0]\n",
      "ModeResult(mode=array([0, 3, 1, 0, 1], dtype=int64), count=array([3, 2, 2, 1, 1], dtype=int64))\n"
     ]
    }
   ],
   "source": [
    "from scipy import stats\n",
    "import numpy as np\n",
    "\n",
    "arr = np.array([[0, 0, 1, 1, 1], [0, 3, 3, 2, 3], [0, 3, 1, 0, 2]])\n",
    "res = stats.mode(arr, axis=1).mode # axis=1 指按行记录来选\n",
    "print(res)\n",
    "\n",
    "res = stats.mode(arr) # default axis=0\n",
    "print(res)\n",
    "\n",
    "b = np.array([['a','b'],['a', 'c']])\n",
    "# Argument `a` is not recognized as numeric. Support for input that cannot be coerced to a numeric array was \n",
    "#   deprecated in SciPy 1.9.0 and removed in SciPy 1.11.0. Please consider `np.unique`\n",
    "#print('eeeee', stats.mode(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "3111c737-dafb-4e0e-b9a4-479df7f28d88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "list sliced: 1\n",
      "np-array sliced by list:\n",
      " [[4 5 6]\n",
      " [4 5 6]]\n",
      "np-array sliced by Boolean:\n",
      " [[1 2 3]]\n",
      "picking 1st dimension: [1 4]\n",
      "picking 1nd dimension: [2 5]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([[1,2,3],[4,5,6]])\n",
    "a = [1,1]\n",
    "print('list sliced:', a[0])\n",
    "print('np-array sliced by list:\\n', x[a])\n",
    "# 与x[a]不同  指的是是否挑出对应的\n",
    "b = [True, False]\n",
    "print('np-array sliced by Boolean:\\n', x[b])\n",
    "print('picking 1st dimension:', x[:,0])\n",
    "print('picking 1nd dimension:', x[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "06d35341-6824-4c11-a7e7-ed5c01f2846c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "unique values: [1 2 3 4 5]\n",
      "unique idx: [1 2 0 6 7]\n",
      "[2 0 1 1 2 0 3 4 4]\n",
      "(array([1, 2, 3, 4, 5]), array([2, 2, 2, 1, 2], dtype=int64))\n",
      "multi-dim unique, axis=0:\n",
      " [[1 0 0]\n",
      " [1 4 6]\n",
      " [2 3 4]]\n",
      "multi-dim unique, axis=1:\n",
      " [[0 0 1]\n",
      " [0 0 1]\n",
      " [3 4 2]\n",
      " [3 4 2]\n",
      " [4 6 1]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# https://blog.csdn.net/weixin_74850661/article/details/132816963\n",
    "def t_unique():\n",
    "    arr = np.array([3, 1, 2, 2, 3, 1, 4, 5, 5])\n",
    "    \n",
    "    # 获取唯一值数组(这里不用写成 [1],是因为没有像下面那样指定xxx=True)\n",
    "    unique_values = np.unique(arr)\n",
    "    print('unique values:', unique_values)\n",
    "    \n",
    "    # 获取唯一值的索引数组\n",
    "    unique_indices = np.unique(arr, return_index=True)[1]\n",
    "    print('unique idx:', unique_indices)\n",
    "\n",
    "    # 获取逆向索引数组，其中包含原始数组中的每个元素在唯一值数组中的索引\n",
    "    inverse_indices = np.unique(arr, return_inverse=True)[1]\n",
    "    print(inverse_indices)\n",
    "    \n",
    "    # 获取唯一值的出现次数数组\n",
    "    value_counts = np.unique(arr, return_counts=True)\n",
    "    print(value_counts)\n",
    "\n",
    "    # 多维数组，指定axis，将axis下的看作一个整体进行唯一值search\n",
    "    # 对于二维数组，axis=0 是寻找完全不同的行\n",
    "    # 对于二维数组，axis=1 是寻找完全不同的列\n",
    "    a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4], [2, 3, 4], [1, 4, 6]]) # 有2对相同的行 ， 无相同的列\n",
    "    # 输出时按行或列排序\n",
    "    print('multi-dim unique, axis=0:\\n', np.unique(a, axis=0))\n",
    "    print('multi-dim unique, axis=1:\\n', np.unique(a, axis=1))\n",
    "\n",
    "t_unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a465681f-0b15-4510-858b-f5b513727065",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
